package com.dataaccess.dataaccess.web.web;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseController;
import com.dataaccess.dataaccess.common.utils.appkit.FirefightingiotKit;
import com.dataaccess.dataaccess.config.rocketmq.UserProducer;
import com.dataaccess.dataaccess.model.AlarmAllDeviceDataChanged;
import com.dataaccess.dataaccess.support.annotaion.Action;
import com.dataaccess.dataaccess.support.annotaion.At;
import com.dataaccess.dataaccess.support.model.RespEntity;
import com.dataaccess.dataaccess.support.utils.MyLogger;
import com.dataaccess.dataaccess.web.mapper.AlarmAllDeviceDataChangedMapper;
import com.dataaccess.dataaccess.web.service.SatwellWirelessGatewayService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.UUID;

/**
 * @Author: YangYu
 * @ClassName: SatwellWirelessGatewayController
 * @Date: Created in 9:35 2021/12/15
 * @Description: 赛特威尔无线物联网关服务
 */
@Action
@RequestMapping("siterwell")
public class SatwellWirelessGatewayController extends BaseController {

    @Resource
    public MyLogger logger;
    @Resource
    private AlarmAllDeviceDataChangedMapper alarmAllDeviceDataChangedMapper;
    @Resource
    UserProducer userProducer;
    @Resource
    SatwellWirelessGatewayService satwellWirelessGatewayService;

    /**
     * @Author YangYu
     * @Description GSM03pro无线物联网关
     * @Date Created in 9:50 2021/12/15
     * @Param [jsonParam]
     */

    @At(value = "device/deviceDataChanged", method = RequestMethod.POST)
    public RespEntity GSM03PRO(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        logger.info("GSM03PRO"+"------"+jsonParam);
        //报警数据
        AlarmAllDeviceDataChanged alarmAllDeviceDataChanged = new AlarmAllDeviceDataChanged();
        alarmAllDeviceDataChanged.setId(UUID.randomUUID().toString());
        alarmAllDeviceDataChanged.setJsondata(jsonParam.toString());
        alarmAllDeviceDataChanged.setType("GSM03PRO");
        alarmAllDeviceDataChangedMapper.insert(alarmAllDeviceDataChanged);
        //--------------------解析数据--------------------------------------------
        String code = jsonParam.getStr("code");
        //返回指令
        String returnCode = "";
        if (code.length() == 8){
            returnCode = code.substring(0,2)+"00";
            return RespEntity.success(returnCode);
        }else if (code.substring(22,24).equals("01")){
            returnCode = code.substring(0,24)+"0800"+systemTime();
            returnCode = returnCode+checkSum(returnCode);
            return RespEntity.success(returnCode);
        }else {
            returnCode = code.substring(0,24)+"0100";
            returnCode = returnCode+checkSum(returnCode);
        }
        jsonParam.put("deviceFlag","GSM03PRO");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            return satwellWirelessGatewayService.GSM03PRO(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success(returnCode);
    }
    /**
     * @Author YangYu
     * @Description 返回的校验和
     * @Date Created in 9:43 2021/5/19
     * @Param [code]
     */

    public static String checkSum(String code){
        int num = 0;
        for (int j = 0; j <code.length() ; j = j+2) {

            num = num+hex16To10(code.substring(j,j+2));
        }
        String nums = getHex16(num);
        nums = nums.substring(nums.length() -2,nums.length());
        return nums;
    }

    /**
     * @Author YangYu
     * @Description 返回系统十六进制时间
     * @Date Created in 10:22 2021/12/15
     * @Param []
     */

    public static String systemTimeHex(){
        String[] waterTime = DateUtil.now().split(" ");
        System.out.println(waterTime);
        String date = waterTime[0];
        String[] dateCode = date.split("-");
        String ms = waterTime[1];
        String[] msCode = ms.split(":");
        //20211215100756
        String hexTime = hex10To16s(Integer.parseInt(dateCode[0].substring(0,2)))+hex10To16s(Integer.parseInt(dateCode[0].substring(2,4)))
                +hex10To16s(Integer.parseInt(dateCode[1]))+hex10To16s(Integer.parseInt(dateCode[2]))+hex10To16s(Integer.parseInt(msCode[0]))
                +hex10To16s(Integer.parseInt(msCode[1]))+hex10To16s(Integer.parseInt(msCode[2]));
        return hexTime;
    }
    /**
     * @Author YangYu
     * @Description 返回系统时间
     * @Date Created in 10:22 2021/12/15
     * @Param []
     */

    public static String systemTime(){
        String[] waterTime = DateUtil.now().split(" ");
        System.out.println(waterTime);
        String date = waterTime[0];
        String[] dateCode = date.split("-");
        String ms = waterTime[1];
        String[] msCode = ms.split(":");
        //20211215100756
        String hexTime = dateCode[0]+dateCode[1]+dateCode[2]+msCode[0]+msCode[1]+msCode[2];
        return hexTime;
    }
    /**
     * @Author YangYu
     * @Description 十六进制转（整型）
     * @Date Created in 9:34 2021/5/19
     * @Param [strHex]
     */

    public static int hex16To10(String strHex) {
        BigInteger lngNum = new BigInteger(strHex, 16);
        return lngNum.intValue();
    }
    /**
     * @Author YangYu
     * @Description int类型转16进制
     * @Date Created in 15:51 2021/4/22
     * @Param [number]
     */

    public static String getHex16(int number) {
        int i = 0;
        char[] S = new char[100];
        String str = "";
        if (number == 0) {
            str = "0";
        } else {
            while (number != 0) {
                int t = number % 16;
                if (t >= 0 && t < 10) {
                    S[i] = (char) (t + '0');
                    i++;
                } else {
                    S[i] = (char) (t + 'A' - 10);
                    i++;
                }
                number = number / 16;
            }

            for (int j = i - 1; j >= 0; j--) {
                //System.out.print(S[j]);
                str = str + S[j];
            }
        }
        return str;
    }
    
    /**
     * @Author YangYu
     * @Description 十进制转十六进制
     * @Date Created in 10:48 2021/12/15
     * @Param [value]
     */
    
    public static String hex10To16s(int value) {
        String hex = Integer.toHexString(value);
        if (hex.length() == 1){
            hex = "0"+hex;
        }
        return hex;
    }
    public static void main(String[] args) {

        System.out.println(systemTime());
        String c = "fe040102332309100a17d82700000000640000000000000002";
        System.out.println(checkSum(c));
    }
}
